/* 
 * Advanced Foundation Classes
 * Copyright (C) 2000/2004  Fabio Rotondo - Andrea Galimberti
 *  
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *  
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *  
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include "../easygui.h"
#include "../widgets.h"

struct _my_list_data
{
	char * f1;
	char * f2;
	char * f3;

	short  selected;
};

typedef struct _my_list_data ListData;

ArrayMaster * my_list;

void add_to_list ( EasyGUIContext * context, char * f1, char * f2, char * f3 )
{
	ListData * data;

	if ( ( data = afc_malloc ( sizeof ( ListData ) ) ) == NULL )
		return;

	data->f1 = afc_string_dup ( f1 );
	data->f2 = afc_string_dup ( f2 );
	data->f3 = afc_string_dup ( f3 );
	data->selected = FALSE;

	afc_array_master_add ( my_list, data, AFC_ARRAY_MASTER_ADD_TAIL );

	afc_easygui_widget_execute ( context, "main_clist", "set_fore_color", 255, 255, 255, AFC_EASYGUI_ARG_END );

	afc_easygui_widget_execute ( context, "main_clist", "add", data->f1, data->f2, data->f3, data, AFC_EASYGUI_ARG_END );
	afc_easygui_widget_execute ( context, "main_clist", "select", AFC_EASYGUI_CLIST_LAST_ROW, AFC_EASYGUI_CLIST_SELECT, AFC_EASYGUI_ARG_END );

	
}

int btn_add_click ( DynamicClass * dc, EasyGUIContext * context )
{
	char * f1, * f2, * f3;

	afc_easygui_widget_get_attr ( context, "entry_name",    AFC_EASYGUI_WIDGET_ATTR_VALUE, &f1 );
	afc_easygui_widget_get_attr ( context, "entry_surname", AFC_EASYGUI_WIDGET_ATTR_VALUE, &f2 );
	afc_easygui_widget_get_attr ( context, "entry_email",   AFC_EASYGUI_WIDGET_ATTR_VALUE, &f3 );

	add_to_list ( context, f1, f2, f3 );	

	afc_easygui_widget_set_attr ( context, "entry_name",       AFC_EASYGUI_WIDGET_ATTR_VALUE, "" );
	afc_easygui_widget_set_attr ( context, "entry_surname",    AFC_EASYGUI_WIDGET_ATTR_VALUE, "" );
	afc_easygui_widget_set_attr ( context, "entry_email",      AFC_EASYGUI_WIDGET_ATTR_VALUE, "" );

	afc_easygui_widget_set_attr ( context, "entry_name",      AFC_EASYGUI_WIDGET_ATTR_FOCUS, ( void * ) TRUE );

	return ( AFC_ERR_NO_ERROR );
}

int btn_sel_all ( DynamicClass * dc, EasyGUIContext * context )
{
	afc_easygui_widget_execute ( context, "main_clist", "select", AFC_EASYGUI_CLIST_ALL_ROWS, AFC_EASYGUI_CLIST_SELECT, AFC_DYNAMIC_CLASS_ARG_END );
	
	return ( AFC_ERR_NO_ERROR );
}

int btn_sel_none ( DynamicClass * dc, EasyGUIContext * context )
{
	afc_easygui_widget_execute ( context, "main_clist", "select", AFC_EASYGUI_CLIST_ALL_ROWS, AFC_EASYGUI_CLIST_UNSELECT, AFC_DYNAMIC_CLASS_ARG_END );
	
	return ( AFC_ERR_NO_ERROR );
}

int btn_dump_sel ( DynamicClass * dc, EasyGUIContext * context )
{
	ListData * data;

	printf ( "Selected items...\n" );
	
	data = afc_array_master_first ( my_list );
	while ( data )
	{
		if ( data->selected )
			printf ( "%s - %s - %s\n", data->f1, data->f2, data->f3 );

		data = afc_array_master_next ( my_list );
	}


	return ( AFC_ERR_NO_ERROR );
}

int entry_activate ( DynamicClass * dc, EasyGUIContext * context, const char * text )
{
	char * s;

	afc_dynamic_class_execute ( dc, "get_attr", AFC_EASYGUI_WIDGET_ATTR_NAME, &s, AFC_DYNAMIC_CLASS_ARG_END );
	fprintf ( stderr, "Entry activated! Text: %s - Name: %s\n", text, s );

	return ( AFC_ERR_NO_ERROR );
}

int clist_act ( DynamicClass * dc, EasyGUIContext * context, void * info )
{
	ListData * data = info;

	fprintf ( stderr, "ROW double click! %s\n", data->f1 );

	return ( AFC_ERR_NO_ERROR );
}

void clist_sel ( DynamicClass * dc, EasyGUIContext * context, short sel, void * info )
{
	ListData * data = info;

	//printf ( "Selection: %s - %d\n", data->f1, sel );
	
	data->selected = sel;
}

int chk_show_titles ( DynamicClass * dc, EasyGUIContext * context, short sel )
{
	fprintf ( stderr, "Show Titles: %d\n", sel );

	afc_easygui_widget_set_attr ( context, "main_clist", AFC_EASYGUI_CLIST_ATTR_SHOW_TITLES, ( void * ) ( int ) sel );

	return ( AFC_ERR_NO_ERROR );
}

int btn_clear ( DynamicClass * dc, EasyGUIContext * context )
{
	ListData * data;
	
	afc_easygui_widget_execute ( context, "main_clist", "clear", AFC_EASYGUI_ARG_END );

	data = afc_array_master_first ( my_list );
	while ( data )
	{
		afc_string_delete ( data->f1 );
		afc_string_delete ( data->f2 );
		afc_string_delete ( data->f3 );
		afc_free ( data );

		data = afc_array_master_next ( my_list );
	}

	afc_array_master_clear ( my_list );

	return ( 0 );
	
}

int main ( int argc, char * argv[] )
{
	AFC * afc = afc_new (); 
	EasyGUI * easygui = afc_easygui_new ( afc );
	int afc_res = 0;

	if ( easygui == NULL ) 
	{
	  fprintf ( stderr, "Init of class EasyGUI failed.\n" );
	  return ( 1 );
	}

	my_list = afc_array_master_new ();

	afc_easygui_init ( easygui );

	afc_easygui_set_gui_from_file ( easygui, "gui_tclist.txt" );

	if ( ( afc_res = afc_easygui_show ( easygui ) ) != AFC_ERR_NO_ERROR ) {
		fprintf ( stderr, "res: %x\n", afc_res );
	}
	else afc_easygui_main ( easygui );

	afc_easygui_delete ( easygui );

	afc_delete ( afc );

	return ( 0 ); 
}
